package com.tuju.jetpackfirstdemo.ui.opengl

import android.app.ActivityManager
import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.tuju.jetpackfirstdemo.R
import android.opengl.GLSurfaceView
import android.os.Handler
import android.os.Message
import com.tuju.jetpackfirstdemo.ui.opengl.renderer.GLRendererTest
import com.tuju.jetpackfirstdemo.ui.opengl.renderer.STLGLRenderer
import com.tuju.jetpackfirstdemo.ui.opengl.renderer.TriangleRanderer
import com.tuju.jetpackfirstdemo.utils.showToast


class OpenGLActivity : AppCompatActivity() {

    private var supportsEs2 = false
    private var glSurfaceView: GLSurfaceView? = null

    private var rotateDegreen = 0f
    private var glRenderer: STLGLRenderer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkSupported()

        if (supportsEs2) {
            glSurfaceView = GLSurfaceView(this)
            glRenderer = STLGLRenderer(this@OpenGLActivity)
//            glSurfaceView?.setRenderer(glRenderer)
//            glSurfaceView?.setRenderer(GLRendererTest())//设置渲染的Renderer
            glSurfaceView?.setRenderer(TriangleRanderer())//设置渲染的Renderer
            setContentView(glSurfaceView)
        } else {
            setContentView(R.layout.activity_open_gl)
            showToast("当前设备不支持OpenGL ES 2.0!")
        }

    }

    private fun checkSupported() {
        val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val configurationInfo = activityManager.deviceConfigurationInfo
        supportsEs2 = configurationInfo.reqGlEsVersion >= 0x2000

        val isEmulator = Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1
                && (Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.startsWith("unknown")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.MODEL.contains("Android SDK built for x86"))
        supportsEs2 = supportsEs2 || isEmulator
    }

    private val handler = object : Handler() {
        override fun handleMessage(msg: Message?) {
            rotate(rotateDegreen)
        }
    }

    fun rotate(degree: Float) {
        glRenderer?.rotate(degree)
        glSurfaceView?.invalidate()
    }

    override fun onResume() {
        super.onResume()
        if (glSurfaceView != null) {
            glSurfaceView?.onResume()
            //不断改变rotateDegreen值，实现旋转
            object : Thread() {
                override fun run() {
                    while (true) {
                        try {
                            Thread.sleep(100)
                            rotateDegreen += 5
                            handler.sendEmptyMessage(0x001)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                    }
                }
            }.start()
        }
    }


    override fun onPause() {
        super.onPause()
        glSurfaceView?.onPause()
    }


}
